package service.impl.v2;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.AddressVOMapper;
import dao.v2.MemberVOMapper;
import model.AddressVO;
import model.MemberVO;
import service.v2.AddressService;
import service.v2.MemberCartServise;
@Service
public class AddressServiceImpl implements AddressService {	
	@Autowired
	private AddressVOMapper dao;
	
	@Autowired
	private MemberCartServise memberCartServise;
	
	@Autowired
	private MemberVOMapper memberDao;

	public List<AddressVO> getAddressListByMemberId(String member_id) {
		return dao.getAddressListByMemberId(member_id);
	}

	public void insertAddresss(AddressVO addressVO) {
		dao.insertAddresss(addressVO);
		if("Y".equals(addressVO.getDefault_yn())){
			dao.cancelOtherDefault(addressVO);
		}
	}
	
	public int getAddressNum(String member_id) {	
		return dao.getAddressNum(member_id);
	}

	public AddressVO getAddressById(String id) {
		return dao. getAddressById(id);
	}

	public void updateAddresss(AddressVO addressVO) {
		dao.updateAddresss(addressVO);
		if("Y".equals(addressVO.getDefault_yn())){
			dao.cancelOtherDefault(addressVO);
		}
	}

	public void deleteAddresss(AddressVO addressVO) {
		dao.deleteAddresss(addressVO) ;
//		int num = dao.getDefaultNum(addressVO);
//		if(num==0){
//			dao.setDefaultAddress(addressVO);
//		}
	}

	public AddressVO getDefaultAddress(String member_id) {
		return dao.getDefaultAddress(member_id);
	}

	public AddressVO getAlternativeAddress(String address_id) {
		return dao.getAlternativeAddress(address_id);
	}

	@Override
	public String tempAddress(AddressVO defaultAddress) {
		MemberVO city_info=new MemberVO();
		MemberVO membervo=memberDao.getCityParentCity(defaultAddress.getCity_id());
		city_info =memberCartServise.packageAddress(membervo);
		String address = defaultAddress.getName()+","
							+city_info.getCity_name1()
							+"-"+city_info.getCity_name2()
							+"-"+city_info.getCity_name3()
							+" "+defaultAddress.getAddress();
		
		
		return address;
	}


	
	@Override
	public List<AddressVO> getAddressListEnByMemberId(String member_id) {
		return dao.getAddressListEnByMemberId(member_id);
	}
	@Override
	public void insertAddresssEn(AddressVO addressVO) {
		dao.insertAddresssEn(addressVO);
		if("Y".equals(addressVO.getDefault_yn())){
			dao.cancelOtherDefaultEn(addressVO);
		}
	}
	@Override
	public AddressVO getAddressByIdEn(String id) {
		return dao.getAddressByIdEn(id);
	}
	@Override
	public void deleteAddresssEn(AddressVO addressVO) {
		dao.deleteAddresssEn(addressVO);
	}
	@Override
	public void updateAddresssEn(AddressVO addressVO) {
		dao.updateAddresssEn(addressVO);
		if("Y".equals(addressVO.getDefault_yn())){
			dao.cancelOtherDefaultEn(addressVO);
		}
	}
	@Override
	public AddressVO getDefaultAddressEn(String member_id) {
		return dao.getDefaultAddressEn(member_id);
	}
}
